//
//  BKUserShowNoContentView.m
//  bestkeep
//
//  Created by windCould on 16/12/21.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserShowNoContentView.h"
#import "BKMyCommon.h"

@interface BKUserShowNoContentView ()

@property (nonatomic,strong)UIImageView * logoImgView;
@property (nonatomic,copy)NSString * text;
@property (nonatomic,strong)UIButton * retryButton;
@property (nonatomic,strong)UIButton * reloadButton;
@property (nonatomic,strong)UIView * messageview;
@property (nonatomic,strong)UIImage * logoImage;
@property (nonatomic,strong)UILabel * LogoLable;
@property (nonatomic,strong)UILabel * msLabel;
@property (nonatomic,copy)NSString  * logoLableText;


@end



@implementation BKUserShowNoContentView


-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.messageview = [[UIView alloc] init];
        self.messageview.tag = 1111;
        [_messageview setBackgroundColor:BK_MY_COLOR_03];
        
        [_messageview setFrame:self.bounds];
        [_messageview setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
        [self addSubview:_messageview];
        
        self.logoImage = [UIImage imageNamed:@""];
        self.logoImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0,self.logoImage.size.width, self.logoImage.size.height)];
        self.logoImgView.hidden = YES;
        [_logoImgView setImage:_logoImage];
        [_messageview addSubview:_logoImgView];
        
        _LogoLable = [[UILabel alloc] init];
        [_messageview addSubview:_LogoLable];
        _LogoLable.alpha = 0.3;
        _LogoLable.textColor = HexColor(0xf7f7f7);
        self.logoLableText = @"\U0000e619";
        
        
        self.msLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 21)] ;
        _msLabel.font = [UIFont boldSystemFontOfSize:16.0f];
        _msLabel.backgroundColor = [UIColor clearColor];
        _msLabel.textColor = HexColor(0x999999);
        _msLabel.numberOfLines = 2;
        _msLabel.textAlignment = NSTextAlignmentCenter;
        
        self.retryButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _retryButton.frame = CGRectMake(0, 0, 80, 30);
        [_retryButton addTarget:self action:@selector(retryButtonClcik) forControlEvents:UIControlEventTouchUpInside];
        _retryButton.titleLabel.font = [UIFont boldSystemFontOfSize:15];
        _retryButton.clipsToBounds = YES;
        _retryButton.layer.cornerRadius = 3;
        [_messageview addSubview:_retryButton];
        
        [self.messageview addSubview:_msLabel];
        
        self.reloadButton =[UIButton buttonWithType:UIButtonTypeCustom];
        _reloadButton.titleLabel.font=[UIFont fontWithName:@"iconfont" size:32];
        _reloadButton.clipsToBounds= YES;
        _reloadButton.titleLabel.text=@"\U0000e619";
        _reloadButton.titleLabel.textColor = BK_MY_COLOR_05;
        [_reloadButton addTarget:self action:@selector(reloadButtonClcik) forControlEvents:UIControlEventTouchUpInside];
        [_messageview addSubview:_reloadButton];
        [self addTapGesture];
        
        [_reloadButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_messageview.mas_right).mas_offset(-30);
            make.bottom.equalTo(_messageview.mas_bottom).mas_offset(-30);
        }];
    }
    return self;
}

- (void)hide:(BOOL)animated
{
    if (animated) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.30];
    }
    
    self.alpha = 0.0f;
    
    if (animated) {
        [UIView commitAnimations];
    }
    
    [self performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:0.40];
}



-(void)retryButtonClcik{
    
    [self hide:YES];
    
    self.retryBlock?self.retryBlock():nil;
}
-(void)reloadButtonClcik{
   
    [self hide:YES];
    self.retryBlock?self.retryBlock():nil;
}
-(void)addTapGesture{
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageViewTap:)];
    [self addGestureRecognizer:tapGesture];
}
-(void)messageViewTap:(UITapGestureRecognizer *)tap{
    if (self.retryBlock) {
        return;
    }
    
   // if ([self.messageReloadDelegate respondsToSelector:@selector(reloadData)]) {
       
        
       // [self.messageReloadDelegate reloadData];
        
   // }else{
        
        if (self.retryBlock) {
            
            self.retryBlock();
        }
    
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self updateMessageFrameByViewFrame:self.bounds];
}

-(void)updateMessageFrameByViewFrame:(CGRect)frame
{
    CGFloat top;
    UIImage* image = self.logoImage;
    if (image) {
        top = frame.size.height / 2 - image.size.height;
    }else{
        top = frame.size.height/2 - _LogoLable.height;
    }
    
    [_logoImgView setHidden:(top < 0)];
    if(top < 0)
    {
        _msLabel.centerY = frame.size.height / 2;
    }
    else
    {
        _logoImgView.bottom = frame.size.height / 2-64;
        _LogoLable.bottom = frame.size.height/2 - 64;
        _msLabel.top = _LogoLable.bottom+ 10;
        _retryButton.top = _msLabel.bottom+ 10;
        _retryButton.centerX = _msLabel.centerX;
    }
    
    _logoImgView.centerX = _msLabel.centerX = frame.size.width / 2;
    _LogoLable.centerX = _msLabel.centerX = frame.size.width/2;
}

- (void)setLogoImage:(UIImage *)logoImage
{
    _logoImage = logoImage;
    _logoImgView.image = logoImage;
    [_logoImgView sizeToFit];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

-(void)setLogoLableText:(NSString *)logoLableText{
   
    _logoLableText = logoLableText;
    _LogoLable.text = logoLableText;
    [self setLogoLable];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

-(void)setLogoLable{
   /*
    UIFont *iconfont =[UIFont fontWithName:@"iconfont" size:60];
    _LogoLable.font=iconfont;
    _LogoLable.textAlignment = NSTextAlignmentCenter;
    _LogoLable.text =self.logoLableText;
    _LogoLable.textColor = HexColor(0x999999);
    CGSize titleLabelsize = [_LogoLable.text boundingRectWithSize:CGSizeMake(SCREENWIDTH-20, 20) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:iconfont} context:nil].size;
    _LogoLable.frame = CGRectMake(0, 0, SCREENWIDTH, titleLabelsize.height);
    
    */
}

-(void)setText:(NSString *)aText
{
    _msLabel.text = aText;
    _msLabel.width = self.width;
    _msLabel.textColor = RGBCOLOR(184, 184, 184,1);
    _msLabel.font = [UIFont systemFontOfSize:15];
    [self setNeedsLayout];
    [self layoutIfNeeded];
}


+ (BKUserShowNoContentView *)showEmptyMsgViewTo:(UIView *)view
                                     logoImgStr:(NSString *)logoImgStr
                            emptyText:(NSString *)emptyText
                          buttonTitle:(NSString *)buttonTitle
                          reloadTitle:(NSString *)reloadTitle
                          animationed:(BOOL)animated{
    
    BKUserShowNoContentView * msgView = [[BKUserShowNoContentView alloc] initWithFrame:CGRectMake(0,0,view.frame.size.width,view.frame.size.height)];
    if ([buttonTitle isEqualToString:@"刷新"]) {
        
        msgView.logoImgView.hidden = NO;
        msgView.logoImgView.frame = CGRectMake(0, 0,80, 80);
        msgView.logoImgView.image = [UIImage imageNamed:logoImgStr];
        msgView.backgroundColor = [UIColor clearColor];
        
    }else {
        
        msgView.logoImgView.hidden = YES;
    }
    msgView.text = emptyText;
    
    msgView.retryButton.backgroundColor = [UIColor clearColor];
    [msgView.reloadButton setTitleColor:RGBACOLOR(184, 184, 184,1) forState:(UIControlStateNormal)];
    msgView.retryButton.bounds = CGRectMake(0, 0, 100, 30);
    
    if (buttonTitle.length > 0) {
        
        msgView.retryButton.hidden = NO;
        [msgView.retryButton setTitle:buttonTitle forState:UIControlStateNormal];
        [msgView.retryButton setTitleColor:RGBACOLOR(27, 188, 155,1) forState:UIControlStateNormal];
        msgView.retryButton.layer.borderColor = RGBACOLOR(27, 188, 155,1).CGColor;
        msgView.retryButton.layer.borderWidth = 1.0f;
        msgView.retryButton.titleLabel.font = [UIFont systemFontOfSize:18];
        msgView.retryButton.layer.cornerRadius = 5;
        msgView.retryButton.layer.masksToBounds = YES;
        
    }else {
        
        msgView.retryButton.hidden = YES;
    }
    
    if (reloadTitle.length > 0) {
        
        msgView.reloadButton.hidden=NO;
        
        [msgView.reloadButton setTitle:reloadTitle forState:UIControlStateNormal];
        [msgView.reloadButton setTitleColor:BK_MY_COLOR_05 forState:(UIControlStateNormal)];
        
        msgView.reloadButton.layer.cornerRadius = 15;
        msgView.reloadButton.layer.masksToBounds = YES;
        
    }else{
        
        msgView.reloadButton.hidden=YES;
    }
    
    [msgView setFrame:CGRectMake(msgView.frame.origin.x, 0, msgView.frame.size.width, msgView.frame.size.height)];
    
    msgView.logoLableText = logoImgStr;
    
    [view addSubview:msgView];
    
    [msgView show:animated];
    
    return msgView;
}

-(void)show:(BOOL)animated
{
    self.alpha = 0.0f;
    if (animated) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.30];
    }
    
    self.alpha = 1.0f;
    
    if (animated) {
        [UIView commitAnimations];
    }
}


@end
